/**
* string to hex
*/
function stringToHex(str) {
var val = "";
for (var i = 0; i < str.length; i++) {
if (val === "") {
val = str.charCodeAt(i).toString(16);
} else {
val += "," + str.charCodeAt(i).toString(16);
}
}
return val;
};
/**
* hex to string
*/
function hexToString(str) {
var val = "";
var arr = str.split(",");
for (var i = 0; i < arr.length; i++) {
val += String.fromCharCode(parseInt(arr[i], 16));
}
return val;
};
/**
* xor36加密
*
* @param {*} cypherKey 金鑰
* @param {*} plainText 一般字串
* @returns 密文
*/
function xor36Encode(cypherKey, plainText) {
if (cypherKey == "" || plainText == "") {
window.alert("沒有輸入金鑰值或明文字串");
} else {
var cypher = ""; // 密文
var encodeStr = stringToHex(plainText);
for (var i = 0; i < encodeStr.length; i++) {
// 若key的長度小於原文,則以循環給的方式
const keyPointer = i % cypherKey.length;
const dec = parseInt((encodeStr[i]).charCodeAt(0) ^ (cypherKey[keyPointer].charCodeAt(0)));
/*
轉成36進制
由於ascii code最大到255,轉換成36進制後最多只會有兩位數,因此才抓最後兩位數
補零是因為有些值只會有一位,需要補零成兩位數,這樣解密才不會出問題
*/
const decStr = ("00" + dec.toString(36)).slice(-2);
cypher += decStr;
}
return cypher;
}
};
/**
* xor36解密
*
* @param {*} cypherKey 金鑰
* @param {*} cypherText 密文
* @returns 一般字串
*/
function xor36Decode(cypherKey, cypherText) {
if (cypherKey == "" || cypherText == "") {
window.alert("沒有輸入金鑰值或密文字串");
} else {
var plainText = "";
const cypherArray = [];
var i;
// 將密文兩個一組放進陣列
for (i = 0; i < cypherText.length; i = i + 2) {
cypherArray.push(cypherText[i] + cypherText[i + 1]);
}
for (i = 0; i < cypherArray.length; i++) {
const hex = cypherArray[i];
// 將密文從36進制轉回10進制
const dec = parseInt(hex, 36);
const keyPointer = i % cypherKey.length;
const asciiCode = dec ^ (cypherKey[keyPointer]).charCodeAt(0);
plainText += String.fromCharCode(asciiCode);
}
plainText = hexToString(plainText);
return plainText;
}
};
function urlEncode(encodeText) {
var txt = encodeURIComponent(encodeText);
//txt = txt.replace("%20", "+");
return txt;
};
function urlDecode(decodeText) {
//decodeText = decodeText.replace("+", "%20");
var txt = decodeURIComponent(decodeText);
return txt;
};
(1) 引用jquery.min.js、js-base64.min.js、jquery.base64.js
(2) JS碼如下
/**
* base64編碼
*
* @param {*} plainText 一般字串
* @returns base64字串
*/
function base64Encode(plainText) {
$.base64.utf8encode = true;
var base64Cypher = Base64.encode(plainText);
return base64Cypher;
};
/**
* base64解碼
*
* @param {*} base64Cypher base64字串
* @returns 一般字串
*/
function base64Decode(base64Cypher) {
$.base64.utf8encode = true;
var plainText = Base64.decode(base64Cypher);
return plainText;
};
(1) 引用crypto-js.js
(2) JS碼如下
/**
* AES-256-CBC 加密
*
* @param {*} words 明文
* @param {*} key 金鑰
* @param {*} iv initial vector
* @returns hex 密文
*/
function cryptojsEncrypt(words, key, iv) {
words = CryptoJS.enc.Utf8.parse(words);
iv = CryptoJS.enc.Utf8.parse(iv);
key = CryptoJS.enc.Utf8.parse(key);
var option = {
iv: iv,
mode: CryptoJS.mode.CBC,
padding: CryptoJS.pad.Pkcs7
};
let encrypted = CryptoJS.AES.encrypt(words, key, option);
encrypted = encrypted.ciphertext.toString(); // 預設 ciphertext.toString() 返回 hex 字串
return encrypted;
};
/**
* AES-256-CBC 解密
*
* @param {*} words hex 密文
* @param {*} key 金鑰
* @param {*} iv initial vector
* @returns 明文
*/
function cryptojsDecrypt(words, key, iv) {
words = CryptoJS.enc.Hex.parse(words);
words = CryptoJS.enc.Base64.stringify(words);
iv = CryptoJS.enc.Utf8.parse(iv);
key = CryptoJS.enc.Utf8.parse(key);
var option = {
iv: iv,
mode: CryptoJS.mode.CBC,
padding: CryptoJS.pad.Pkcs7
};
let decrypted = CryptoJS.AES.decrypt(words, key, option);
decrypted = decrypted.toString(CryptoJS.enc.Utf8);
return decrypted;
};
function encrypt(iv, key, plainText) {
var cypherText = cryptojsEncrypt(plainText, key, iv);
return cypherText;
};
function decrypt(iv, key, cypherText) {
var plainText = cryptojsDecrypt(cypherText, key, iv);
return plainText;
};
/**
* 計算字串的 byte 數
*
* @return 回傳 byte 數
*/
String.prototype.getStringBytes = function() {
return this.replace(/[^\x00-\xff]/g,"xx").length;
};
/**
* 計算字串的 byte 數
*
* @return 回傳 byte 數
*/
String.prototype.getStringChars = function() {
var cnt = 0;
for (var i = 0; i < this.length; i++) {
var c = this.charAt(i);
if (/^[\u0000-\u00ff]$/.test(c)) {
cnt++;
} else {
cnt += 2;
}
}
return cnt;
};
var yesDateYear = "2023";
var yesDateMonth = "12";
var yesDateDay = "31";
var iDateYear = window.document.getElementById("iDateYear");
var iDateMonth = window.document.getElementById("iDateMonth");
var iDateDay = window.document.getElementById("iDateDay");
for (var i = 0; i < iDateYear.length; i++) {
if (iDateYear.options[i].value == yesDateYear) {
iDateYear.options[i].selected = true;
}
}
for (var j = 0; j < iDateMonth.length; j++) {
if (iDateMonth.options[j].value == yesDateMonth) {
iDateMonth.options[j].selected = true;
}
}
for (var k = 0; k < iDateDay.length; k++) {
if (iDateDay.options[k].value == yesDateDay) {
iDateDay.options[k].selected = true;
}
}
HTML 碼如下:
<select name="dateYear" id="iDateYear"></select>
<select name="dateMonth" id="iDateMonth"></select>
<select name="dateDate" id="iDateDay"></select>